<?php
namespace Home\Controller;

use Think\Controller;
use Biz\JPusher;
use Biz\AliyunVod;
use OSS\OssClient;
use OSS\OssException;
use OSS\Core\OssUtil;
use Ruben\Aliyun\Live;
use Ruben\Tcyun\TcIM;
use Ruben\Aliyun\Mts;

class IndexController extends Controller
{
    public function index()
    {
        $words = '成人视频|操|';
        $words = explode('|', trim($words, '|'));
        // echo str_replace('成人视频', '**', '视频');
        echo filter_badwords('视频', $words);
        // $baidu = new \Biz\Baidu();
        // $baidu->tran();
        // echo 123;
        // $config = C('sdk.oss');
        // dump($config);
        // $mts = new Mts($config);
        // $mediaInfo = $mts->queryMediaList('33fb2959858845479f8119b86ecef088');
        // $mediaInfo = $mediaInfo->MediaList->Media[0];
        
        // dump($mediaInfo);
    }

    public function trans()
    {

    }

    public function conf()
    {
        $settings = D('Setting')->select();
        $dict = [];
        foreach ($settings as $setting) {
            C('sdk.' . $setting['k'], array_merge(C('sdk.' . $setting['k']), unserialize($setting['v'])));
        }

        dump(C('sdk'));
    }

    public function player()
    {
        $this->display();
    }

    public function score()
    {
        D('Account/Users')->addScore('post', 201);
    }

    public function redis()
    {
        $redis = redis();
        var_dump($redis->exists('rank:20170823'));
        // dump($redis->zrevrange('sss', 0, 30, 'WITHSCORES'));
        // dump($redis->hgetall('ruben'));
        // $redis = new \Redis();
        // $redis->connect('192.168.33.10', 6379);
        // $redis->auth('srb123');
        // echo $redis->ping();
    }

    public function sendGift()
    {
        $giftId = I('get.gift', 27);
        $config = C('sdk.tcim');
        $tcim = new \Ruben\Tcyun\TcIM($config);
        $from = D('Users')->find(208);
        $from = transform('User', 'simple', $from);
        $gift = D('Gift')->find($giftId);
        $gift = transform('Gift', 'index', $gift);
        $tcim->sendGift('@TGS#aR2GRG4EM', $from, $gift, 2);
    }

    public function querystate()
    {
        $config = C('sdk.tcim');
        $tcim = new TcIM($config);
        $ret = $tcim->queryState(['203', '201', '225', '255']);
        dump($ret);

    }

    public function tcim()
    {
        // $config = C('sdk.tcim');
        // $tcim = new \Ruben\Tcyun\TcIM($config);
        
        // $users = D('Users')->where('user_id = 208')->select();
        // foreach ($users as $user) {
        //     $chatroom = $tcim->createChatroom($user['user_id'], $user['user_id'] . ' chatroom');
        //     D('Users')->where(['user_id' => $user['user_id']])->setField('chatroom', $chatroom);
        // }

        // echo $tcim->createChatroom('201', 'accc');
        // echo $tcim->genCmdSig();
    }

    public function forbideStream()
    {
        $config = C('sdk.aliyunlive');
        $aliyun = new Live($config);
        $aliyun->forbidLiveStream('show_201_1503048279');
    }

    public function resumeStream()
    {
        $config = C('sdk.aliyunlive');
        $aliyun = new Live($config);
        $aliyun->resumeLiveStream('show_201_1503048279');
    }

    public function changeRole()
    {
        $config = C('sdk.tcim');
        $tcim = new \Ruben\Tcyun\TcIM($config);
        $tcim->changeRole('205', '@TGS#3KTTVF4EJ', 'Admin');
    }


    public function importUser()
    {
        // $config = C('sdk.tcim');
        // $tcim = new \Ruben\Tcyun\TcIM($config);
        // $users = D('Account/Users')->select();
        // foreach ($users as $user) {
        //     $s = $tcim->accountImport($user['user_id'], $user['nickname'], $user['face']);
        //     dump($s);
        // }
        
    }

    // public function index()
    // {
    //     $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    // }

    public function mediaInfo()
    {
        $config = C('sdk.aliyun');
        $mts = new \Ruben\Aliyun\Mts($config);
        $ret = $mts->queryMediaList('67ba5582e76a453799ec62e3b717203e');
        dump($ret);
    }

    public function getDescribeLiveStreamSnapshotInfo()
    {
        $config = C('sdk.aliyunlive');
        $aliyun = new Live($config);
        $aliyun->getDescribeLiveStreamSnapshotInfo('show_201_1503048279', 1503048279, time());
    }

    public function refreshMedia($mediaId)
    {
        D('Helper/Media')->fill($mediaId);
    }

    public function push()
    {
        $msg = I('get.msg');
        $to = I('get.to');
        $jpush = new JPusher();
        $jpush->push($msg, $to);
    }

    public function snap()
    {
        $config = C('sdk.aliyun');
        $aliyun = new \Biz\Aliyun($config);
        $resp = $aliyun->queryMediaListByURL();

    }

    public function upload()
    {
        $config = C('sdk.aliyun');
        $vod = new AliyunVod($config);
        $info = $vod->createUploadVideo();
        $ossConfig = C('sdk.oss');
        $client = new OssClient($ossConfig['access_key_id'], $ossConfig['access_key_secret'], $ossConfig['endpoint']);
        $client->putObject('video.catrongzi.com', $info->UploadAddress, $_FILES['']);


    }
}
